<?php
//模板管理
class TemplateUtils{
	
	public static function test(){
		return "模板管理";
	}


	/*********
		2019/4/11
		上传背景图片
	**********/
	public static function uploadImage($params){
		$db = Zend_Registry::get('tplDB');

		$name = $_FILES['imgFile']['name'];
		
		$insertSql = "insert into shine_template_element (`element_name`) values('".$name."')";
		$db->query($insertSql);
		$lastInsertId = $db->lastInsertId();

		$imgExtList = array_reverse(explode(".",$_FILES["imgFile"]["name"]));
		$saveImgPath = "/admin/images/upload/$lastInsertId".".".$imgExtList[0];
		$uploadPath = $_SERVER['DOCUMENT_ROOT'].$saveImgPath;
		$ret = move_uploaded_file($_FILES["imgFile"]["tmp_name"],$uploadPath);

		$updateSql = "update shine_template_element set element_path='".$saveImgPath."' where element_id='".$lastInsertId."'";
		$db->query($updateSql);
		return $lastInsertId;
	}


	/***********
		2019/4/11
		删除模板素材
	***********/
	public static function deleteImage($params){
		$db = Zend_Registry::get('tplDB');
		$ids = Zend_Json::decode($params['ids']);
		if(count($ids)>0){
			$sql = "select * from shine_template_element where element_id in(".join(",",$ids).")";
			$res = $db->query($sql);
			$row = $res->fetchAll();
			foreach($row as $item){
				$path = $_SERVER['DOCUMENT_ROOT'].$item['element_path'];
				if(file_exists($path)){
					unlink($path);
				}
			}

			$deleteSql = "delete from  shine_template_element where element_id in(".join(",",$ids).")";
			$db->query($deleteSql);
		}

		return Zend_Json::encode($ids);

	}


	/************
		2019/4/11
		获取字段对应关系
	**********/
	public static function templateFieldMap($params){
		
		$url = "http://".$_SERVER['HTTP_HOST']."/api.php/columns/hospital_patients";
		$data = Zend_Json::decode(file_get_contents($url))['columns'];
		for($x=1;$x<=10;$x++){
			$data[] = array(
				'name'=>"Description$x"	
			);
		}
		return Zend_Json::encode($data);
	}


	/***********
		2019/4/16
		导出模板数据
	**********/
	public static function exportThemeJson($params){
		$db = Zend_Registry::get('tplDB');
		if(!isset($params['templateId'])){
			return Zend_Json::encode($params);
		}
		$templateId = $params['templateId'];
		$sql = "select * from shine_template_cmp where template_id='$templateId'";
		$res = $db->query($sql);
		$row = $res->fetchAll();
				
		$templateSql = "select * from shine_template_list where template_id='$templateId'";
		$templateRes = $db->query($templateSql);
		$templateRow = $templateRes->fetchAll();

		$templateCmpList = array();
		$templateImage = 1;
		
		$backgroundImgId = 0;
		foreach($row as $item){
			if(intval($item['cmp_type_id'])==1){
				$backgroundImgId = $item['cmp_background_img'];
			}
			$templateCmpList[] = $item;
		}

		
		$bgSql = "select * from shine_template_element where element_id='$backgroundImgId'";
		$bgRow = $db->fetchAll($bgSql);

		if(count($bgRow)>0){
			$templateImage= $bgRow[0]['element_name'];
		}

		$templateJson = array(
			'template'=>$templateRow[0],
			'children'=>$templateCmpList,
			'cmp_background_img'=>$templateImage
		);
		

		$tpl = Zend_Json::encode($templateJson);
		
		$path = $_SERVER['DOCUMENT_ROOT']."/admin/export/$templateId.txt";
		file_put_contents($path,$tpl);

		return Zend_Json::encode($templateRow[0]);

	}


	/***********
		2019/3/1
		导入模板数据
	***********/
	public static  function importThemeJson($params){
		$db = Zend_Registry::get('tplDB');
		$path = $_FILES['template']['tmp_name'];

		$templateObj = json_decode(trim(file_get_contents($path)),true);
		
		if(isset($templateObj['cmp_background_img'])){
			

			$tpltime = date("Y-m-d H:i:s",time());
			$template_type = isset($templateObj['template'])?$templateObj['template']['template_type']:24;
			$resolution_id = isset($templateObj['template'])?$templateObj['template']['resolution_id']:9;
			$template_name = isset($templateObj['template'])?$templateObj['template']['template_name']:"新模板-".time();

			$insertTplSql = "insert into shine_template_list(`template_name`,`template_type`,`create_time`,`resolution_id`) values('$template_name','$template_type','$tpltime','$resolution_id')";
			$db->query($insertTplSql);
			
			$templateId = $db->lastInsertId();
			$parentId = 0;
			foreach($templateObj['children'] as $item){
				$item['template_id'] = $templateId;
				$id = self::addComponent2DB($item);
				if($item['cmp_type_name'] == "epanel"){
					$parentId = $id;
				}
			}

			$updateTplSql = "update shine_template_cmp set cmp_parent_id='$parentId' where template_id='$templateId' and cmp_id!='$parentId'";
			$db->query($updateTplSql);

		}

		$setting = array(
			"success"=>true,
			"msg"=>"导入成功！"
		);
		
		return Zend_Json::encode($setting);
	}

	/***********
		2019/3/1
		添加组件到数据库
	************/
	public static function addComponent2DB($item){
 
		$db = Zend_Registry::get('tplDB');
		$cmp_name = isset($item['cmp_name'])?$item['cmp_name']:"面板";
		$cmp_parent_id = isset($item['cmp_parent_id'])?$item['cmp_parent_id']:0;
		$cmp_level = isset($item['cmp_level'])?$item['cmp_level']:1;
		$cmp_type = isset($item['cmp_type'])?$item['cmp_type']:1;
		$template_id = isset($item['template_id'])?$item['template_id']:0;
		$cmp_type_name = isset($item['cmp_type_name'])?$item['cmp_type_name']:"epanel";
		$cmp_layout = isset($item['cmp_layout'])?$item['cmp_layout']:1;
		$cmp_font_size = isset($item['cmp_font_size'])?$item['cmp_font_size']:24;
		$cmp_background = isset($item['cmp_background'])?$item['cmp_background']:"red";
		$cmp_color = isset($item['cmp_color'])?$item['cmp_color']:"white";
		$cmp_width = isset($item['cmp_width'])?$item['cmp_width']:200;
		$cmp_height = isset($item['cmp_height'])?$item['cmp_height']:100;
		$cmp_region = isset($item['cmp_region'])?$item['cmp_region']:"center";
		$cmp_left = isset($item['cmp_left'])?$item['cmp_left']:0;
		$cmp_top = isset($item['cmp_top'])?$item['cmp_top']:0;
		$cmp_word_direction = isset($item['cmp_word_direction'])?$item['cmp_word_direction']:"center";
		$cmp_word_between = isset($item['cmp_word_between'])?$item['cmp_word_between']:0;
		$cmp_word_count = isset($item['cmp_word_count'])?$item['cmp_word_count']:5;
		$cmp_word_bold = isset($item['cmp_word_bold'])?$item['cmp_word_bold']:"normal";
		$cmp_value = isset($item['cmp_value'])?$item['cmp_value']:"";
		$cmp_background_img = isset($item['cmp_background_img'])?$item['cmp_background_img']:0;
		$cmp_order_index = isset($item['cmp_order_index'])?$item['cmp_order_index']:0;
		$cmp_text_align = isset($item['cmp_text_align'])?$item['cmp_text_align']:"left";
		$cmp_word_type = isset($item['cmp_word_type'])?$item['cmp_word_type']:"ZH";


		$cmpInsertSql = "insert into shine_template_cmp(`cmp_name`,`cmp_parent_id`,`cmp_level`,`cmp_type_id`,`template_id`,`cmp_layout_id`,`cmp_font_size`,`cmp_background`,`cmp_color`,`cmp_width`,`cmp_height`,`cmp_region`,`cmp_left`,`cmp_top`,`cmp_word_direction`,`cmp_word_between`,`cmp_word_count`,`cmp_word_bold`,`cmp_background_img`,`cmp_value`,`cmp_order_index`,`cmp_text_align`,`cmp_word_type`,`cmp_type_name`) values ('$cmp_name','$cmp_parent_id','$cmp_level','$cmp_type','$template_id','$cmp_layout','$cmp_font_size','$cmp_background','$cmp_color','$cmp_width','$cmp_height','$cmp_region','$cmp_left','$cmp_top','$cmp_word_direction','$cmp_word_between','$cmp_word_count','$cmp_word_bold','$cmp_background_img','$cmp_value','$cmp_order_index','$cmp_text_align','$cmp_word_type','$cmp_type_name')";
		$db->query($cmpInsertSql);

		$checkCmpSql = "select * from shine_template_cmp order by cmp_id desc limit 1";
		$checkCmpRow = $db->fetchAll($checkCmpSql);
		return $checkCmpRow[0]['cmp_id'];
	}


}